package com.geologyoa.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.XMLWriter;

import com.geologyoa.domain.RoleAndPow;
import com.geologyoa.domain.UserInfo;
import com.geologyoa.service.SystemManager;
import com.geologyoa.service.SystemManagerImpl;

public class SearchPowerInfoServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
        doPost(request,response);
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		SystemManager sys=new SystemManagerImpl();
		Integer roleid=Integer.parseInt(request.getParameter("ROLEID"));
		List<RoleAndPow> powroleList=sys.SerachPower(roleid);
		Document document=DocumentHelper.createDocument();
		if(powroleList!=null)
		{
			Element fwlistElement =document.addElement("powroleList");
			fwlistElement.addAttribute("size", (new StringBuilder(String.valueOf(powroleList.size()))).toString());
			RoleAndPow roleAndPow;
			Element fwElement;
			for(Iterator iterator=powroleList.iterator();iterator.hasNext();)
			{
				roleAndPow = (RoleAndPow)iterator.next();
				fwElement=fwlistElement.addElement("powroleInfo");
				fwElement.addAttribute("ROLENAME", roleAndPow.getRolenames());
				fwElement.addAttribute("ROLEFUNCTION", roleAndPow.getRolefunction());
				fwElement.addAttribute("FUNCTIONNAME", roleAndPow.getFunctionname());
				fwElement.addAttribute("FUNCTIONID", roleAndPow.getFunctionid());
				
			}
		}
		StringWriter xmlout = new StringWriter();
		XMLWriter output = new XMLWriter(xmlout);
		output.write(document);
		response.setContentType("text/html;charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Expires", "0");
		response.setHeader("Pragma", "No-cache");
		response.getWriter().write(xmlout.toString());
	}

}
